package com.pinecone.controller;

import java.util.List;
import java.util.Map;


import com.pinecone.common.Constant.RoleConstant;
import com.pinecone.common.annotation.CurrentUser;
import com.pinecone.common.exception.MinerException;
import com.pinecone.dto.UserDetailsDTO;
import com.pinecone.entity.CusHostEntity;
import com.pinecone.service.CusHostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import com.pinecone.entity.CusPrincipalEntity;
import com.pinecone.service.CusPrincipalService;
import com.pinecone.common.utils.PageUtils;
import com.pinecone.common.utils.Query;
import com.pinecone.common.utils.R;




/**
 * 客户主体表
 * 
 * @author hushangjie
 * @email 979783618@qq.com
 * @date 2017-09-27 21:17:59
 */
@RestController
@RequestMapping("cus_principal")
public class CusPrincipalController {
	@Autowired
	private CusPrincipalService cusPrincipalService;
    @Autowired
    private CusHostService cusHostService;

    /**
     *小主播恢复初始密码(只有机构管理员才能操作)
     * @param customerId
     * @return
     */
    @PreAuthorize("hasRole('ROLE_ORGMASTER')")
    @GetMapping("/reset_pwd")
    public R resetPwd(@RequestParam(value = "customer_id",required = true) Integer customerId){
        cusPrincipalService.resetPwd(customerId);
        return R.ok();
    }

    /**
     * 删除主播账号(系统管理员和学校管理员可用)
     * @param currentUser
     * @param hostId
     * @return
     */
    @PreAuthorize("hasAnyRole('ROLE_SYSADMIN','ROLE_ORGMASTER')")
    @GetMapping("/forbid/{cus_id}")
    public R forbidHost(@CurrentUser UserDetailsDTO currentUser , @PathVariable(value = "cus_id",required = true) Long hostId){
        CusHostEntity hostEntity = cusHostService.queryObject(hostId);
        if (hostEntity == null){
            throw new MinerException("该主播不存在");
        }
        if (currentUser.getRolename().equals(RoleConstant.ORGMASTER)){
            //机构管理员只能禁用本机构的主播
            if (hostEntity.getOrgId() != currentUser.getOrgId()){
                throw new MinerException("无权操作",403);
            }
        }
        cusPrincipalService.forbidHost(hostEntity.getCustomerId());
        return R.ok();
    }

	
	/**
	 * 列表
	 */
	@GetMapping("")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		if (params.get("page") != null && params.get("limit")!= null){
			List<CusPrincipalEntity> cusPrincipalList = cusPrincipalService.queryList(query);
			int total = cusPrincipalService.queryTotal(query);
			
			PageUtils pageUtil = new PageUtils(cusPrincipalList, total, query.getLimit(), query.getPage());
			
			return R.ok().put("page", pageUtil);
		}else{
			List<CusPrincipalEntity> cusPrincipalList = cusPrincipalService.queryList(query);
			return R.ok().put("list", cusPrincipalList);
		}
		
	}
	
	
	/**
	 * 信息
	 */
	@GetMapping("/{cusId}")
	public R info(@PathVariable("cusId") Integer cusId){
		CusPrincipalEntity cusPrincipal = cusPrincipalService.queryObject(cusId);
		
		return R.ok().put("cusPrincipal", cusPrincipal);
	}
	
	/**
	 * 保存
	 */
	@PostMapping("")
	public R save(@RequestBody CusPrincipalEntity cusPrincipal){
		cusPrincipalService.save(cusPrincipal);
		
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@PutMapping("")
	public R update(@RequestBody CusPrincipalEntity cusPrincipal){
		cusPrincipalService.update(cusPrincipal);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@DeleteMapping("")
	public R delete(@RequestBody Integer[] cusIds){
		cusPrincipalService.deleteBatch(cusIds);
		
		return R.ok();
	}
	
}
